package com.sg.service.biz.bff.impl;

import com.sg.common.exception.Assert;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.bff.req.BffCheckCaptchaValidComReqDto;
import com.sg.dto.biz.bff.req.BffQueryGetCaptchaDetailReqDto;
import com.sg.dto.biz.bff.res.BffCheckCaptchaValidComRespDto;
import com.sg.dto.biz.bff.res.BffQueryGetCaptchaDetailRespDto;
import com.sg.dto.integration.component.SendVerificationCodeReqDto;
import com.sg.dto.integration.component.SendVerificationCodeRespDto;
import com.sg.dto.integration.framework.QueryAppDetailReqDto;
import com.sg.dto.integration.framework.QueryAppDetailRespDto;
import com.sg.dto.integration.framework.QueryUserDetailReqDto;
import com.sg.dto.integration.framework.QueryUserDetailRespDto;
import com.sg.service.biz.bff.CaptchaService;
import com.sg.service.biz.bff.nb.NbCaptcha;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {
    @Resource
    com.sg.service.biz.framework.UserService fwBaseUserClient;
    @Resource
    com.sg.service.biz.framework.BaseService fwBaseBaseClient;
    @Resource
    com.sg.service.biz.component.MessageService fwCompMessageClient;
    @Resource
    com.sg.service.biz.framework.CaptchaService fwBaseCaptchaClient;
    @Resource
    NbCaptcha nbCaptcha;
    @Resource
    private RedisUtil redisUtil;

    /**
     * B1-发送验证码（公共）[1188]
     * gen by moon at 8/10/2022, 3:33:26 AM
     */
    @Trace(operationName = "B1-发送验证码（公共）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryGetCaptchaDetailRespDto queryGetCaptchaDetail(BffQueryGetCaptchaDetailReqDto reqDto) {


        //virtualUsage D1-2查询个人账号详情  2151
        QueryUserDetailRespDto queryUserDetailRespDto = null;
        QueryUserDetailReqDto queryUserDetailReqDto = new QueryUserDetailReqDto();
        if (reqDto != null) {
            queryUserDetailReqDto.setPhone(reqDto.getPhone());//sourceId:27068_1_2151
        }

        /*D1-2查询个人账号详情[1032]   */

        queryUserDetailRespDto = fwBaseUserClient.queryUserDetail(queryUserDetailReqDto).getData();


        if ((queryUserDetailRespDto != null)) {
            //if(D1-2查询个人账号详情.出参 不等于空 )  2152

            QueryAppDetailRespDto queryAppDetailRespDto = null;
            QueryAppDetailReqDto queryAppDetailReqDto = new QueryAppDetailReqDto();
            queryAppDetailReqDto.setUniqueSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:65166_1_2590

            /*D1-2查询联盟应用详情[541]   */

            queryAppDetailRespDto = fwBaseBaseClient.queryAppDetail(queryAppDetailReqDto).getData();


            SendVerificationCodeRespDto sendVerificationCodeRespDto = null;
            if (queryAppDetailRespDto != null) {
                SendVerificationCodeReqDto sendVerificationCodeReqDto = new SendVerificationCodeReqDto();
                if (reqDto != null) {
                    sendVerificationCodeReqDto.setPhone(reqDto.getPhone());//sourceId:64884_1_2591
                    sendVerificationCodeReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:64885_1_2591
                }
                if (queryAppDetailRespDto != null) {
                    sendVerificationCodeReqDto.setAppId(queryAppDetailRespDto.getAppId());//sourceId:65179_1_2591
                }

                /*D2-发送短信验证码[1190]   */
                Assert.isNull(sendVerificationCodeReqDto.getPhone(), "B1-发送验证码（公共）-D2-发送短信验证码-绑定手机不能为空", false);
                Assert.isNull(sendVerificationCodeReqDto.getBehaviorTypeCode(), "B1-发送验证码（公共）-D2-发送短信验证码-行为类型编码不能为空", false);
                Assert.isNull(sendVerificationCodeReqDto.getAppId(), "B1-发送验证码（公共）-D2-发送短信验证码-应用ID不能为空", false);
                sendVerificationCodeRespDto = fwCompMessageClient.sendVerificationCode(sendVerificationCodeReqDto).getData();


            }
//processBranchName:正常结束 ,processBranchId:2592

        }
        BffQueryGetCaptchaDetailRespDto retData = new BffQueryGetCaptchaDetailRespDto();


        return retData;
    }

    /**
     * B1-检查验证码是否有效(公共)[2275]
     * gen by moon at 8/10/2022, 3:38:42 AM
     */
    @Trace(operationName = "B1-检查验证码是否有效(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffCheckCaptchaValidComRespDto checkCaptchaValidCom(BffCheckCaptchaValidComReqDto reqDto) {


        CheckCaptchaValidRespDto checkCaptchaValidRes_1 = null;
//步骤0: D1-检测验证码是否有效（特殊方法） - checkCaptchaValid
        //ModelCode: checkCaptchaValid
        CheckCaptchaValidRespDto checkCaptchaValidRes = null;
        CheckCaptchaValidReqDto checkCaptchaValidReq = new CheckCaptchaValidReqDto();
        if (reqDto != null) {
            checkCaptchaValidReq.setPhone(reqDto.getPhone());//sourceId:66786_1
            checkCaptchaValidReq.setVerificationCode(reqDto.getVerificationCode());//sourceId:66787_1
        }

        /*D1-检测验证码是否有效（特殊方法）[1185]  用于检测用户前端录入验证码是否有效 */

        checkCaptchaValidRes = nbCaptcha.checkCaptchaValid(checkCaptchaValidReq);

        checkCaptchaValidRes_1 = checkCaptchaValidRes;

        BffCheckCaptchaValidComRespDto retData = new BffCheckCaptchaValidComRespDto();
        if (checkCaptchaValidRes_1 != null) {
            retData.setIsInvalid(checkCaptchaValidRes_1.getIsInvalid());//sourceId:66791_1
        }


        return retData;
    }
    //
}
